package com.leyou.item.services.impl;

import com.leyou.common.enums.ExceptionEnums;
import com.leyou.common.exception.LyException;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.services.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date 2018/12/15  16:48
 * @Author JIE
 */
@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    /**
     * 根据分类id查询所有规格组
     * @param cid
     * @return
     */
    @Override
    public List<SpecGroup> queryGroupByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        List<SpecGroup> SpecGroups = specGroupMapper.select(specGroup);
        if(CollectionUtils.isEmpty(SpecGroups)){
            throw new LyException(ExceptionEnums.SPEC_GROUP_NOT_FOND);
        }
        return SpecGroups;
    }

    /**
     * 新增加商品规格组
     * @param cid
     * @param name
     */
    @Override
    public void saveSpecGroup(Long cid, String name) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        specGroup.setName(name);
        int count = specGroupMapper.insert(specGroup);
        if(count!=1){
            throw new LyException(ExceptionEnums.SPEC_GROUP_SAVE_ERROR);
        }
    }

    /**
     * 修改商品规格组
     * @param group
     */
    @Override
    public void updateSpecGroup(SpecGroup group) {
        int count = specGroupMapper.updateByPrimaryKey(group);
        if(count!=1){
            throw new LyException(ExceptionEnums.SPEC_GROUP_UPDATA_ERROR);
        }
    }

    /**
     * 根据id删除商品规格组
     * @param id
     */
    @Override
    public void deleteSpecGroupById(Long id) {
        int count = specGroupMapper.deleteByPrimaryKey(id);
        if(count!=1){
            throw new LyException(ExceptionEnums.SPEC_GROUP_DELETE_ERROR);
        }
    }

    /**
     * 根据商品规格组id或者分类id或者searching查询组下的规格参数
     * @param gid
     * @return
     */
    @Override
    public List<SpecParam> queryParamList(Long gid, Long cid, Boolean searching){
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        List<SpecParam> specParamList = specParamMapper.select(specParam);
        if(CollectionUtils.isEmpty(specParamList)){
            throw new LyException(ExceptionEnums.SPEC_PARAM_NOT_FOND);
        }
        return specParamList;
    }

    /**
     * 新增商品规格参数
     * @param specParam
     */
    @Override
    public void saveSpecParam(SpecParam specParam) {
        int count = specParamMapper.insert(specParam);
        if(count!=1){
            throw new LyException(ExceptionEnums.SPEC_PARAM_SAVE_ERROR);
        }
    }

    /**
     * 修改商品规格参数
     * @param specParam
     */
    @Override
    public void updateSpecParam(SpecParam specParam) {
        int count = specParamMapper.updateByPrimaryKey(specParam);
        if(count!=1){
            throw new LyException(ExceptionEnums.SPEC_PARAM_UPDATA_ERROR);
        }
    }

    /**
     * 根据规格参数id删除
     * @param id
     */
    @Override
    public void deleteSpecParamById(Long id) {
        int count = specParamMapper.deleteByPrimaryKey(id);
        if(count!=1){
            throw new LyException(ExceptionEnums.SPEC_PARAM_DELETE_ERROR);
        }
    }

    /**
     * 根据分类的id查询参数组及其组内的参数
     * @param cid
     * @return
     */
    @Override
    public List<SpecGroup> queryListByCid(Long cid) {
        //查询规格组
        List<SpecGroup> specGroups = queryGroupByCid(cid);
       //查询当前分类下的参数
        List<SpecParam> specParamList = queryParamList(null, cid, null);
        //先把规格参数变成map，map的key是规格参数组id,map的值是组下的所有参数
        Map<Long,List<SpecParam>> map=new HashMap<>();
        for (SpecParam specParam : specParamList) {
            if(!map.containsKey(specParam.getGroupId())){
                //组id在map中不存在，新增一个list
                map.put(specParam.getGroupId(),new ArrayList<>());
            }
            map.get(specParam.getGroupId()).add(specParam);
        }
        //填充param到group中
        for (SpecGroup specGroup : specGroups) {
            specGroup.setSpecParamList(map.get(specGroup.getId()));
        }
        return specGroups;
    }
}
